package spring.boot.mybatis.plus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import spring.boot.mybatis.plus.model.TUser;
import spring.boot.mybatis.plus.service.TUserService;

import java.util.List;

@RestController
@RequestMapping("/t/user")
public class TUserController {

    @Autowired
    private TUserService tUserService;

    //查询
    @GetMapping("select")
    public List<TUser> select() {
        return tUserService.list();
    }

    //分页查询
    @GetMapping("select/page")
    public IPage<TUser> selectPage() {
        return tUserService.selectUserPage();
    }

    //分页查询
    @GetMapping("select/page/2")
    public IPage<TUser> selectPage2() {
        return tUserService.selectUserPage2();
    }

    //新增
    @PostMapping("insert")
    public boolean insert(@RequestBody(required = false) TUser tUser) {
        if (tUser == null) {
            tUser = new TUser();
            tUser.setUsername("Jack");
            tUser.setAge(18);
        }
        return tUserService.save(tUser);
    }


    //更新整个对象
    @PostMapping("update")
    public boolean update(@RequestBody(required = false) TUser tUser) {
        if (tUser == null) {
            tUser = new TUser();
            tUser.setId(1L);
            tUser.setUsername("Blues");
            tUser.setAge(88);
        }
        return tUserService.updateUser(tUser);
    }

    //更新对象部分信息
    @PostMapping("update/some")
    public boolean updateSome(@RequestBody(required = false) TUser tUser) {
        if (tUser == null) {
            tUser = new TUser();
            tUser.setId(1L);
            tUser.setAge(90);
        }
        return tUserService.updateById(tUser);
    }

    //删除
    @DeleteMapping("delete/{id}")
    public boolean delete(@PathVariable(required = false) Long id) {
        return tUserService.removeById(id);
    }

}
